package com.zxc.services.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zxc.dao.EmpMapper;
import com.zxc.entity.Emp;
import com.zxc.services.EmpServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
  * @author: zxc
  * @return:
  * @time:  2021/5/20 19:32
  * @description:
  */
@Service
@Transactional
public class EmpServicesImpl implements EmpServices {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Emp> getAll() {
        return empMapper.getAll();
    }

    @Override
    public PageInfo<Emp> getByPage(Integer pageNum) {
        PageHelper.startPage(pageNum,5);
        List<Emp> all = empMapper.getAll();
        PageInfo<Emp> pageInfo=new PageInfo<>(all);
        return pageInfo;
    }

    @Override
    public boolean addEmp(Emp emp) {
        if (emp != null) {
            if (empMapper.addEmp(emp)>0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean delEmp(int eno) {
        if (eno>0) {
            if (empMapper.delEmp(eno)>0){
                return true;
            }
        }
        return false;
    }

    @Override
    public Emp getEmpByEno(Integer eno) {
        if (eno>0) {
            return empMapper.getEmpByEno(eno);
        }
        return null;
    }

    @Override
    public boolean updateEmp(Emp emp) {
        if (emp != null) {
            if (empMapper.updateEmp(emp)>0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Emp login(String ename, String pwd) {
        if (!(StringUtils.isEmpty(ename)&&StringUtils.isEmpty(pwd))) {
            Emp empByEname = empMapper.getEmpByEname(ename);
            if (empByEname != null) {
                if (empByEname.getPwd().equals(pwd)) {
                    return empByEname;
                }
            }
        }
        return null;
    }
}
